﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace FYPDemo
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["page"] = "productDetails";

            if (Session["username"] != null)
            {
                addToCart.Enabled = true;
            }

            string productID = Request.QueryString["productID"];

            DataTable dt = ProductManagement.displayProductDetail(productID);

            if (dt.Rows.Count > 0)
            {
                lblProductID.Text = productID;
                lblProductName.Text = dt.Rows[0][2].ToString();
                lblPrice.Text = dt.Rows[0][3].ToString();
                prodImage.ImageUrl = dt.Rows[0][4].ToString();
                lblSuppCode.Text = dt.Rows[0][6].ToString();
            }
        }

        

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("displayProduct.aspx");
        }

        protected void addToCart_Click(object sender, EventArgs e)
        {
            string name = Session["username"].ToString();
            string productID = Request.QueryString["ProductID"];
            double prodPrice = Convert.ToDouble(lblPrice.Text);
            string orderID = "";
            string prefix = "";
            string runNo = "";
            int runningNo = 0;

            int quantity = Convert.ToInt32(ddlQuantity.SelectedValue);
            int prodRedundant = 0;

            prodPrice = prodPrice * quantity;

            ProductManagement.cartManagement();

            DataTable dt = ProductManagement.getLatestCart(name);
            
            if (dt.Rows.Count > 0)
            {
                orderID = dt.Rows[0][0].ToString();
            }

            int currentStock = 0;
            int safeLevel = 0;
            DataTable dt1 = ProductManagement.checkStockLevel(productID);
            
            if(dt1.Rows.Count > 0)
            {
              currentStock = Convert.ToInt32(dt1.Rows[0][0]);
              safeLevel = Convert.ToInt32(dt1.Rows[0][1]);
            }

            if((currentStock - quantity) > 0)
            {

              if((currentStock - quantity) <= safeLevel)
              {
                  //send mail to inform vendor below safe level
                  MessageBox.Show("below safe level");
              }

                if (orderID != "")
                {
                    prodRedundant = ProductManagement.checkRedundantProduct(orderID, productID);

                    if (prodRedundant > 0)
                    {
                    
                        ProductManagement.combineRedundantProduct(orderID, productID, quantity, prodPrice);
                        ProductManagement.deductStock(productID, quantity);
                    }

                    else
                    {
                        ProductManagement.insertOrderDetails(orderID, productID, quantity, prodPrice);
                        ProductManagement.deductStock(productID, quantity);
                    }
                }

                else
                {
                    orderID = ProductManagement.getLatestOrderID();                    

                    if (orderID != "")
                    {
                        string num = orderID.Remove(0, 1);
                        prefix = orderID.Substring(0, 1);
                        runningNo = Convert.ToInt32(num) + 1;
                    }

                    else
                    {
                        runningNo = 1;
                        prefix = "U";
                    }

                    runNo = String.Format("{0:00000}", runningNo);
                    orderID = prefix + runNo;

                    ProductManagement.insertOrder(orderID, name);
                    ProductManagement.insertOrderDetails(orderID, productID, quantity, prodPrice);
                    ProductManagement.deductStock(productID, quantity);
                }
            }

            else
            {
                MessageBox.Show("Not enough stock");
            }
        }
    }
}